package fileExercise;

import java.io.File;
import java.io.FileFilter;
import java.util.Objects;

/**
 * @classname:test
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/15 17:02
 * @version: 0.1
 * @since:1.8
 */
public class test {
    public static void main(String[] args) {
//        Scanner input = new Scanner(System.in);
//        System.out.println("请输入路径");
//        String path = input.next();
        String path = "day16";
        File file  = new File(path);
//        showDic(file,"|-");
        demo1(file,"|-");
    }

    //循环遍历出文件夹的内容并且只输出java结尾的文件
    private static void showDic(File file, String s) {
        Objects.requireNonNull(file);
        if(!file.exists()){
            System.out.println("该文件夹不存在");
            return;
        }
        File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println(s+file1.getName());
            if(file1.isDirectory()){
                showDic(file1,"|"+s);
            }else{
                if(file1.getName().endsWith("java")){
                    System.out.println(s+file1.getName());
                }
            }
        }

    }

    public static void demo1(File directory, String s){
        File[] files = directory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(pathname.isDirectory()) return true;
                return pathname.getName().endsWith("java");
            }
        });

        for (File file : files) {
            System.out.println(s+file.getName());
            if(file.isDirectory()){
                demo1(file,"|"+s);
            }
        }
    }


}
